package controllers

import (
	"github.com/revel/revel"
	"waterblabla/app/models"
)

type Customer struct {
	Controller
}

func (c Customer) AddCustomer() revel.Result {
	type Req struct {
		models.QS       `,inline valid:"Required"`
		models.Contract `json:"contract" valid:"Required"`
		StationID       string `json:"stationID" valid:"Required"`
	}
	var req Req
	// parse
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	// TODO: register in uc
	//if err := models.Register(contract.Customer); err != nil {
	//	return c.RenderResult(err)
	//}

	// get station
	var station models.Station
	if err := models.GetStationByID(req.StationID, req.Qid, &station); err != nil {
		return c.RenderResult(err)
	}
	req.Station = &station.StationProfile

	return c.RenderResult(models.AddCustomer(&req.Contract))
}

func (c Customer) RmCustomer() revel.Result {
	type Req struct {
		models.QS  `,inline valid:"Required"`
		StationID  string `json:"stationID" valid:"Required"`
		ContractID string `json:"contractID" valid:"Required"`
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(models.DelCustomer(req.QS.Qid, req.StationID, req.ContractID))
}

func (c Customer) UpdateCustomer() revel.Result {
	type Req struct {
		models.QS       `,inline valid:"Required"`
		models.Contract `json:"contract" valid:"Required"`
		StationID       string `json:"stationID" valid:"Required"`
	}

	// parse
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	// validate qid && stationID
	var station models.Station
	if err := models.GetStationByID(req.StationID, req.Qid, &station); err != nil {
		return c.RenderResult(err)
	}
	req.Contract.Station = &station.StationProfile
	// modify contract
	return c.RenderResult(models.ModifyCustomer(&req.Contract))
}

func (c Customer) GetCustomers() revel.Result {
	type Req struct {
		models.QS `,inline`
		StationID string `json:"stationID"`
		Count     int    `json:"count" valid:"Required"`
		// TODO: validate int64
		Cursor int64 `json:"cursor" valid:"Required"`
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	var contracts []models.Contract
	if err := models.CustomersOfStation(req.QS.Qid, req.StationID, &contracts); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(contracts)
}

func (c Customer) BindSn() revel.Result {
	type Req struct {
		models.QS `,inline`
		Sn string `json:"sn"  valid:"Required"`	
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(models.ClaimSn(req.QS.Qid, req.Sn))
}

func (c Customer) GetSettings() revel.Result {
	// parse
	var qs models.QS
	if err := models.ParseBodyAndValidate(c.body, &qs); err != nil {
		return c.RenderResult(err)
	}

	var contract models.Contract
	if err := models.GetSettings(qs.Qid, &contract); err != nil {
		return c.RenderResult(err)
	}
	return c.RenderResult(contract)
}

func (c Customer) UpdateSettings() revel.Result {
	type Req struct {
		models.QS       `,inline valid:"Required"`
		models.Contract `json:"contract" valid:"Required"`
	}

	// parse
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	// modify contract
	return c.RenderResult(models.UpdateSettings(req.Qid, &req.Contract))
}
